import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.nashorn.internal.runtime.regexp.joni.ast.StringNode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-08-04
 * Time: 20:41
 */
class User {
    public String username;
    public String password;
}
@WebServlet("/json")
public class JsonServlet extends HttpServlet {
//    使用这个对象就可以把json字符串解析成java对象也可以把java对象转成一个json格式字符串
    private ObjectMapper  objectMapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        通过 post 请求的body 传递过来 一个 json 格式的字符串
        User user = objectMapper.readValue(req.getInputStream(),User.class);
        System.out.println("username="+user.username+",password="+user.password);

        resp.getWriter().write("ok");
    }
}
